package com.example.gzq.mymusictest;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gzq.mymusictest.domain.Person;

/**
 * Created by gzq on 17-7-13.
 */

public class Registered extends BaseStyle {
    private static final String TAG = "Registered";
    public Person person;
    private boolean bool_psw=false,bool_btn=true;
    String mPhone,psw,psw1;
    EditText res_phone;EditText res_name;
    EditText res_password;EditText res_password1;
    EditText res_email;RadioButton male;
    RadioButton female;TextView monitor_edit;
    ImageView phone_img,password_img,password1_img,email_img;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registered);
        localBroadcastManager=LocalBroadcastManager.getInstance(this);
        res_phone=(EditText)findViewById(R.id.res_phone);
        res_name=(EditText)findViewById(R.id.res_name);
        res_password=(EditText)findViewById(R.id.res_password);
        res_password1=(EditText)findViewById(R.id.res_password1);
        res_email=(EditText)findViewById(R.id.res_email);
        male=(RadioButton) findViewById(R.id.male);
        female=(RadioButton)findViewById(R.id.female);
        monitor_edit=(TextView)findViewById(R.id.monitor_edit);
        phone_img=(ImageView)findViewById(R.id.phone_img);
        password_img=(ImageView)findViewById(R.id.password_img);
        password1_img=(ImageView)findViewById(R.id.password1_img);
        email_img=(ImageView)findViewById(R.id.email_img);
        Button submit=(Button)findViewById(R.id.btn_submit);
        editCheck(res_phone,"phone");//监控手机号输入格式是否正确
        editCheck(res_password,"password");
        editCheck(res_password1,"password1");
        editCheck(res_email,"email");
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                psw=res_password.getText().toString();
                psw1=res_password1.getText().toString();
                //两次密码输入相同，返回true
                if (psw.equals(psw1)){bool_psw=true;}
                //获取用户性别
                String res_gender=null;
                if (male.isChecked()){res_gender="男";}
                else if (female.isChecked()){ res_gender="女";}

                person=new Person(res_name.getText().toString(),res_password.getText().toString(),
                        res_gender,res_phone.getText().toString(),res_email.getText().toString());
                Log.d(TAG, "onClick: "+person);
                //判断注册信息是否正确
                if (person.getmGender()!=null&&bool_psw&&bool_btn){
                    addUser(person);//
                    Intent intent=new Intent("com.example.gzq.mymusictest.Registered");
                    intent.putExtra("mNub",person.getmName());
                    //发送本地广播
                    localBroadcastManager.sendBroadcast(intent);
                    Log.d(TAG, "onClick: mNub="+person.getmName());
                    finish();
                }
                else if (!bool_psw) {
                    Toast.makeText(Registered.this, "俩次密码不相同", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(Registered.this, "请输入正确的注册信息", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    //注册成功，向数据库中增加新注册信息
    public void addUser(Person person){
        Person p=new Person();
        p.setmName(person.getmName());
        p.setmPassWord(person.getmPassWord());
        p.setmGender(person.getmGender());
        p.setmTel(person.getmTel());
        p.setmEmail(person.getmEmail());
        p.save();
    }
    //输入内容格式判断
    public void editCheck(EditText editText, final String s){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.d(TAG, "onTextChanged: 1111111111111111");
            }
            @Override
            public void afterTextChanged(Editable editable) {
                mPhone = editable.toString();
                if (s.equals("phone")){
                    if (isId(editable.toString())) {
                        phone_img.setImageResource(R.drawable.success);
                        bool_btn=true;
                    }else {
                        phone_img.setImageResource(R.drawable.wrong);
                        bool_btn=false;
                    }
                }else if(s.equals("password")){
                    if (isPsw(editable.toString())) {
                        password_img.setImageResource(R.drawable.success);
                        bool_btn=true;
                    }else {
                        password_img.setImageResource(R.drawable.wrong);
                        bool_btn=false;
                    }
                }else if (s.equals("password1")){
                    if (isPsw(editable.toString())) {
                        password1_img.setImageResource(R.drawable.success);
                        bool_btn=true;
                    }else {
                        password1_img.setImageResource(R.drawable.wrong);
                        bool_btn=false;
                    }
                }
                else if (s.equals("email")){
                    if (isEmail(editable.toString())) {
                        email_img.setImageResource(R.drawable.success);
                        bool_btn=true;
                    }else {
                        email_img.setImageResource(R.drawable.wrong);
                        bool_btn=false;
                    }
                }
                Log.d(TAG, "afterTextChanged: "+mPhone);
            }
        });
    }
    //手机号格式判断
    public static boolean isId(String idStr) {
        if (idStr == null || "".equals(idStr.trim())) {
            return false;
        } else if (idStr.matches("s*1[3578]\\d{9}")) {
            return true;
        }
        return false;
    }
    //密码格式判断
    public static boolean isPsw(String idStr) {
        Log.d(TAG, "isPsw: "+idStr.toString());
        if (idStr == null || "".equals(idStr.trim())) {
            return false;
        } else if (idStr.matches("[0-9a-zA-Z]{3,10}")) {
            return true;
        }
        return false;
    }
    //邮箱格式判断
    public static boolean isEmail(String idStr) {
        Log.d(TAG, "isPsw: "+idStr.toString());
        if (idStr == null || "".equals(idStr.trim())) {
            return false;
        } else if (idStr.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")) {
            return true;
        }
        return false;
    }
}
